package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Department;
import com.example.entity.DepartmentOrderStatistics;
import com.example.entity.OrderDailyStatistics;
import com.example.entity.WorkOrder;
import com.example.exception.CustomException;
import com.example.mapper.WorkOrderMapper;
import com.example.service.WorkOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Resource
    private WorkOrderMapper workOrderMapper;

    @Override
    public PageInfo<WorkOrder> selectPage(WorkOrder workOrder, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WorkOrder> list = workOrderMapper.selectAll(workOrder);
        return PageInfo.of(list);
    }

    /**
     * 新增
     */
    public void add(WorkOrder workOrder) {
        WorkOrder dbWorkOrder = workOrderMapper.selectByOrderNo(workOrder.getOrderNo());
        if (ObjectUtil.isNotNull(dbWorkOrder)) {
            throw new CustomException(ResultCodeEnum.WORK_ORDER_EXIST_ERROR);
        }
        workOrderMapper.insert(workOrder);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        workOrderMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            workOrderMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(WorkOrder workOrder) {
        WorkOrder dbWorkOrder = workOrderMapper.selectByOrderNo(workOrder.getOrderNo());
        if (ObjectUtil.isNotNull(dbWorkOrder)) {
            throw new CustomException(ResultCodeEnum.WORK_ORDER_EXIST_ERROR);
        }
        workOrderMapper.updateById(workOrder);
    }

    /**
     * 分派
     */
    @Override
    public void fenpai(Department department) {
        Department dbDepartment = workOrderMapper.selectByDeptId(department.getDeptId());
        if (!ObjectUtil.isNotNull(dbDepartment)) {
            throw new CustomException(ResultCodeEnum.DEPARTMENT_NOT_EXIST_ERROR);
        }
        WorkOrder workOrder = new WorkOrder();
        workOrder.setId(department.getId());
        workOrder.setHandleDeptId(department.getDeptId());
        workOrder.setFenpaiTime(LocalDateTime.now());
        workOrderMapper.updateById(workOrder);
    }

    /**
     * 查询7月每天的工单总量、超期率
     */
    @Override
    public List<OrderDailyStatistics> getDailyOrderStatisticsForMonth(int year, int month) {
        String time = year + "-" + month + "-01";
        List<OrderDailyStatistics> list = workOrderMapper.getDailyOrderStatisticsForMonth(time);
        return list;
    }

    /**
     * 查询7月每天的工单总量、超期率
     */
    @Override
    public List<DepartmentOrderStatistics> getDepartmentOrderStatisticsForMonth(int year, int month) {
        String time = year + "-" + month + "-01";
        List<DepartmentOrderStatistics> list = workOrderMapper.getDepartmentOrderStatisticsForMonth(time);
        return list;
    }


}
